package com.neusoft.elmboot.controller;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.elmboot.po.Cart;
import com.neusoft.elmboot.service.CartService;

@RestController
@RequestMapping("/carts")
public class CartController {

	@Autowired
	private CartService cartService;

	@GetMapping("/")
	public List<Cart> listCarts(@RequestParam(required = false) String userId,
			@RequestParam(required = false) Integer businessId) {
		Cart cart=new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(businessId);
		return cartService.listCart(cart);
	}

	@PostMapping("/")
	public int saveCart(Cart cart) throws Exception {
		return cartService.saveCart(cart);
	}

    @PutMapping("/")
    public int updateCart(@RequestParam(required = false) String userId,
			@RequestParam(required = false) Integer businessId,
			@RequestParam(required = false) Integer foodId,
			@RequestParam(required = false) Integer quantity) {
    	Cart cart=new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(businessId);
        cart.setFoodId(foodId);
        cart.setQuantity(quantity);
        return cartService.updateCart(cart);
    }

	@DeleteMapping("/")
	public int removeCart(@RequestParam(required = false) String userId,
			@RequestParam(required = false) Integer businessId,
			@RequestParam(required = false) Integer foodId) throws Exception {
    	Cart cart=new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(businessId);
        cart.setFoodId(foodId);
		return cartService.removeCart(cart);
	}
}
